#include "xxxx.h"

int aes_128_ecb_encode(const char* szSrc,
                       const size_t nSrcLen,
                       char* szDst,
                       const size_t nDstLen,
                       const char* szKey)
{
    AES_KEY aes;
    unsigned char key[AES_BLOCK_SIZE];     // AES_BLOCK_SIZE = 16
    unsigned char iv[AES_BLOCK_SIZE];      // init vector
    unsigned char input_string[1024];
    unsigned char encrypt_string[1024];
    unsigned int  len;                     // encrypt length (in multiple of AES_BLOCK_SIZE)
    unsigned int  i;

    // set the encryption length
    len = 0;

    if ((strlen(szSrc) + 1) % AES_BLOCK_SIZE == 0) {
        len = strlen(szSrc) + 1;
    } else {
        len = ((strlen(szSrc) + 1) / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;
    }

    // set the input string
    strncpy((char*)input_string, szSrc, strlen(szSrc));
    
    // Generate AES 128-bit key
    memcpy(key, szKey, 16);

    // Set encryption key
    for (i=0; i<AES_BLOCK_SIZE; ++i) {
        iv[i] = 0;
    }
    if (AES_set_encrypt_key(key, 128, &aes) < 0) {
        fprintf(stderr, "Unable to set encryption key in AES\n");
        exit(-1);
    }


    // encrypt (iv will change)
    AES_cbc_encrypt(input_string, encrypt_string, len, &aes, iv, AES_ENCRYPT);

    // Set decryption key
    for (i=0; i<AES_BLOCK_SIZE; ++i) {
        iv[i] = 0;
    }

    memcpy(szDst, encrypt_string, len);

    return 0;
}

int aes_128_ecb_decode(const char* szSrc,
                       const size_t nSrcLen,
                       char* szDst,
                       const size_t nDstLen,
                       const char* szKey)
{
  return 0;
}
